package com.ruoyi.api.worker;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.ruoyi.system.utils.TokenUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/api/worker/jiedan")
@Api(tags = "社工端接单设置接口")
public class ApiWorkerJieDanController {

    @Autowired
    private IJiajiaWorkerService workerService;

    @Autowired
    private IJiajiaOrderService orderService;

    @Autowired
    private IJiajiaOrderTimeService orderTimeService;

    @Autowired
    private IJiajiaWorkerJiedanService jiedanService;

    @Autowired
    private IJiajiaWorkerJiedanDetailService detailService;

    @Autowired
    private IJiajiaWorkerTimeXxConfigService timeXxConfigService;

    @Autowired
    private IJiajiaWorkerTimeConfigService timeConfigService;

    @Autowired
    private IJiajiaMessageService messageService;


//    @ApiOperation("获取社工配置时间")
//    @PostMapping("/getFuWuTime")
//    public AjaxResult getFuWuTime(@RequestParam String day)
//    {
//        JiajiaWorker worker = workerService.selectJiajiaWorkerById(TokenUtil.getUid());
//        if (worker == null)return  AjaxResult.notLogin();
//        if (StringUtils.isEmpty(worker.getStarTime()) || StringUtils.isEmpty(worker.getEndTime())){
//            return AjaxResult.error("请先设置开始服务时间与结束服务时间");
//        }
//        if (worker.getCodeAuth().intValue() != 1){
//            return AjaxResult.error("请先进行身份认证");
//        }
//
//        //查询订单时间
//        JiajiaOrder order = new JiajiaOrder();
//        order.getParams().put("type","1");//查询进行中的订单
//        order.setWid(worker.getId());
//        order.getParams().put("day", day);
//        List<JiajiaOrder>  orderList = orderService.selectJiajiaOrderList(order);
//
//        JSONObject resObj = new JSONObject();
//        JSONArray jsonArray = new JSONArray();
//
//        for (JiajiaOrder model : orderList) {
//            JSONObject obj = new JSONObject();
//            JiajiaOrderTime jiajiaOrderTime = new JiajiaOrderTime();
//            //修改预约订单号
//            jiajiaOrderTime.setOrderSn(order.getOrderSn());
//            //查询订单预约列表
//            List<JiajiaOrderTime> orderTimeList = orderTimeService.selectJiajiaOrderTimeList(jiajiaOrderTime);
//            if (ObjectUtil.isNotEmpty(orderTimeList)) {//获取预约时间最早的一个时间
//                obj.put("yuYueTime", orderTimeList.get(0).getStartTime());
//            }
//            obj.put("timeSpan",0);
//            jsonArray.add(obj);
//        }
//
//        resObj.put("isJieDan","0");
//
//        JSONArray noJieDanTime = new JSONArray();
//
//        //查询当前时间接单状态和不接单时间
//        JiajiaWorkerJiedan jiedan = new JiajiaWorkerJiedan();
//        jiedan.setWid(TokenUtil.getUid());
//        jiedan.getParams().put("day",day);
//        List<JiajiaWorkerJiedan> jiedanList = jiedanService.selectJiajiaWorkerJiedanList(jiedan);
//        if (jiedanList.isEmpty()){
//           resObj.put("isJieDan","1");//接单
//        }else {
//            jiedan = jiedanList.get(0);
//            if (jiedan.getStatus().intValue() == 0){
//                resObj.put("isJieDan","0");
//            }else {
//                resObj.put("isJieDan","1");
//            }
//            //查询不接单的时间
//            JiajiaWorkerJiedanDetail detail = new JiajiaWorkerJiedanDetail();
//            detail.setWid(TokenUtil.getUid());
//            detail.setJdid(jiedan.getId());
//            List<JiajiaWorkerJiedanDetail> detailList = detailService.selectJiajiaWorkerJiedanDetailList(detail);
//            for (JiajiaWorkerJiedanDetail model : detailList) {
//                noJieDanTime.add(model);
//            }
//        }
//
//        resObj.put("noJieDanTime",noJieDanTime);
//        resObj.put("haveOrderTime",jsonArray);
//        resObj.put("starTime",worker.getStarTime());
//        resObj.put("endTime",worker.getEndTime());
//
//        //查询消息未读
//        resObj.put("isRead",0);//0不展示小红点 1展示
//
//        long count = PageHelper.count(new ISelect() {
//            @Override
//            public void doSelect() {
//                JiajiaMessage message = new JiajiaMessage();
//                message.setUid(worker.getId());
//                message.setType(1);
//                message.setIslook(0);
//                messageService.selectJiajiaMessageList(message);
//            }
//        });
//        if (count > 0){
//            resObj.put("isRead",1);//0不展示小红点 1展示
//        }
//
//        //更新社工定位
//        if (!StringUtils.isEmpty(TokenUtil.getLat()) && !StringUtils.isEmpty(TokenUtil.getLng())){
//            worker.setLat(TokenUtil.getLat());
//            worker.setLng(TokenUtil.getLng());
//            workerService.updateJiajiaWorker(worker);
//        }
//
//
//        return AjaxResult.success(resObj);
//    }

    @ApiOperation("获取社工配置时间")
    @PostMapping("/getFuWuTime")
    public AjaxResult getFuWuTime(@RequestParam String day)
    {
//        JiajiaWorker worker = workerService.selectJiajiaWorkerById(TokenUtil.getUid());
        JiajiaWorker worker = workerService.selectJiajiaWorkerByIds(TokenUtil.getUid());
        if (worker == null)return  AjaxResult.notLogin();
//        if (StringUtils.isEmpty(worker.getStarTime()) || StringUtils.isEmpty(worker.getEndTime())){
//            return AjaxResult.error("请先设置开始服务时间与结束服务时间");
//        }
        if (worker.getCodeAuth().intValue() != 1){
            return AjaxResult.error("请先进行身份认证");
        }

        //查询订单时间
        JiajiaOrder order = new JiajiaOrder();
        order.getParams().put("type","1");//查询进行中的订单
        order.setWid(worker.getId());
        order.getParams().put("day", day);
        List<JiajiaOrder>  orderList = orderService.selectJiajiaOrderList(order);

        JSONObject resObj = new JSONObject();
        JSONArray jsonArray = new JSONArray();

        for (JiajiaOrder model : orderList) {
//            JSONObject obj = new JSONObject();
            JSONArray obj=new JSONArray();
            JiajiaOrderTime jiajiaOrderTime = new JiajiaOrderTime();
            //修改预约订单号
            jiajiaOrderTime.setOrderSn(model.getOrderSn());
            //查询订单预约列表
            List<JiajiaOrderTime> orderTimeList = orderTimeService.selectJiajiaOrderTimeList(jiajiaOrderTime);
//            if (ObjectUtil.isNotEmpty(orderTimeList)) {//获取预约时间最早的一个时间
//                obj.put("yuYueTime", orderTimeList.get(0).getStartTime());
//            }
//            obj.put("timeSpan",0);
//            jsonArray.add(obj);
            for (JiajiaOrderTime orderTime:orderTimeList) {
                obj.add(orderTime.getTcid());
            }
        }

        resObj.put("isJieDan","0");

        JSONArray noJieDanTime = new JSONArray();
        //社工休息状态
        JiajiaWorkerTimeXxConfig config=new JiajiaWorkerTimeXxConfig();
        config.setWid(TokenUtil.getUid());
        config.setStatus(0);
        config.setDay(day);
        //查询社工休息列表
        List<JiajiaWorkerTimeXxConfig> configList =timeXxConfigService.selectJiajiaWorkerTimeXxConfigList(config);
        if (configList.isEmpty()){
            resObj.put("isJieDan","1");//接单
        }else {
            config=configList.get(0);
        }
            if (config.getStatus().intValue() == 0){
                resObj.put("isJieDan","0");
            }else {
                resObj.put("isJieDan","1");
            }
        for (JiajiaWorkerTimeXxConfig timeXxConfig : configList) {
            JiajiaWorkerTimeConfig timeConfig = timeConfigService.selectJiajiaWorkerTimeConfigById(timeXxConfig.getTcid());
            noJieDanTime.add(timeConfig);
        }
        resObj.put("noJieDanTime",noJieDanTime);
        resObj.put("haveOrderTime",jsonArray);
//        resObj.put("starTime",worker.getStarTime());
//        resObj.put("endTime",worker.getEndTime());

        //查询消息未读
        resObj.put("isRead",0);//0不展示小红点 1展示

        long count = PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                JiajiaMessage message = new JiajiaMessage();
                message.setUid(worker.getId());
                message.setType(1);
                message.setIslook(0);
                messageService.selectJiajiaMessageList(message);
            }
        });
        if (count > 0){
            resObj.put("isRead",1);//0不展示小红点 1展示
        }

        //更新社工定位
        if (!StringUtils.isEmpty(TokenUtil.getLat()) && !StringUtils.isEmpty(TokenUtil.getLng())){
            worker.setLat(TokenUtil.getLat());
            worker.setLng(TokenUtil.getLng());
            workerService.updateJiajiaWorker(worker);
        }


        return AjaxResult.success(resObj);
    }

    @ApiOperation("改变当日接单状态")
    @PostMapping("changeFuWu")
    public AjaxResult changeFuWu(@RequestParam String day){

        JiajiaWorker worker = workerService.selectJiajiaWorkerById(TokenUtil.getUid());
        if (worker.getCodeAuth() == null || worker.getCodeAuth().intValue() != 1){
            return AjaxResult.error("请先进行身份认证");
        }
        //查询今天是否有订单
        JiajiaOrder order = new JiajiaOrder();
        order.getParams().put("day",day);
        order.setWid(worker.getId());
        List<JiajiaOrder> orderList = orderService.selectJiajiaOrderByList(order);
        JiajiaWorker jiedanWorker = workerService.selectJiajiaWorkerById(TokenUtil.getUid());
        if (!(orderList.isEmpty())&& jiedanWorker.getStatus().intValue() == 1){
            return AjaxResult.error("当前有订单无法修改状态");
        }
        if (jiedanWorker.getStatus().intValue()==1){
            jiedanWorker.setStatus(0);
            workerService.updateJiajiaWorker(jiedanWorker);
        }else {
            jiedanWorker.setStatus(1);
            workerService.updateJiajiaWorker(jiedanWorker);
        }

        JiajiaWorkerJiedan jiedan = new JiajiaWorkerJiedan();
        jiedan.getParams().put("day",day);
        jiedan.setWid(TokenUtil.getUid());
        List<JiajiaWorkerJiedan> jiedanList = jiedanService.selectJiajiaWorkerJiedanList(jiedan);
        if (jiedanList.isEmpty()){
            jiedan.setDay(DateUtils.parseDate(day));
            jiedan.setStatus(0);
            jiedanService.insertJiajiaWorkerJiedan(jiedan);
        }else {
            jiedan = jiedanList.get(0);
            //判断状态
            if (jiedan.getStatus()==1){
                jiedan.setStatus(0);
            }else {
                jiedan.setStatus(1);
            }
            jiedanService.updateJiajiaWorkerJiedan(jiedan);
        }

        if (jiedan.getStatus().intValue() == 0){
            JiajiaWorkerJiedanDetail detail = new JiajiaWorkerJiedanDetail();
            detail.setJdid(jiedan.getId());
            List<JiajiaWorkerJiedanDetail> detailList = detailService.selectJiajiaWorkerJiedanDetailList(detail);
            detailService.deleteJiajiaWorkerJiedanDetailByIds(StringUtils.listsToSbStr(detailList,"id"));
        }


        return AjaxResult.success();
    }

    @ApiOperation("改变当前时间段接单状态")
    @PostMapping("changeFuWuDetail")
    public AjaxResult changeFuWuDetail(@RequestParam String day,
                                       @RequestParam String starTime,
                                       @RequestParam String endTime){

        JiajiaWorker worker = workerService.selectJiajiaWorkerById(TokenUtil.getUid());
        if (worker.getCodeAuth().intValue() != 1){
            return AjaxResult.error("请先进行身份认证");
        }

        JiajiaWorkerJiedan jiedan = new JiajiaWorkerJiedan();
        jiedan.getParams().put("day",day);
        jiedan.setWid(TokenUtil.getUid());
        List<JiajiaWorkerJiedan> jiedanList = jiedanService.selectJiajiaWorkerJiedanList(jiedan);

        if (jiedanList.isEmpty()){
            jiedan.setDay(DateUtils.parseDate(day));
            jiedan.setStatus(1);
            jiedanService.insertJiajiaWorkerJiedan(jiedan);
        }else {
            jiedan = jiedanList.get(0);
        }

        JiajiaWorkerJiedanDetail detail = new JiajiaWorkerJiedanDetail();
        detail.setJdid(jiedan.getId());
        detail.setStartime(starTime);
        detail.setWid(TokenUtil.getUid());
        List<JiajiaWorkerJiedanDetail> detailList = detailService.selectJiajiaWorkerJiedanDetailList(detail);
        if (detailList.isEmpty()){
            detail.setEndtime(endTime);
            detailService.insertJiajiaWorkerJiedanDetail(detail);
        }else {
            detailService.deleteJiajiaWorkerJiedanDetailByIds(detailList.get(0).getId().toString());
        }


        return AjaxResult.success();
    }


}
